package com.sskj.wallet.ui.activity;


import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.lib.util.ImageUtil;
import com.sskj.lib.util.TipUtil;
import com.sskj.lib.util.WalletUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.AwardBean;
import com.sskj.wallet.bean.CoinAssetBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.CoinInfoActivityPresenter;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.WALLET_COIN_INFO)//币种信息
public class CoinInfoActivity extends BaseActivity<CoinInfoActivityPresenter> {
    @Autowired
    String code;
    @Autowired
    String address;
    @BindView(R2.id.ivClose)
    ImageView ivClose;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.ivCoin)
    ImageView ivCoin;
    @BindView(R2.id.tvCoin)
    TextView tvCoin;
    @BindView(R2.id.tvAddress)
    TextView tvAddress;
    @BindView(R2.id.ivCopy)
    ImageView ivCopy;
    @BindView(R2.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R2.id.tvRMB)
    TextView tvRMB;
    @BindView(R2.id.slidingTabLayout)
    SlidingTabLayout slidingTabLayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @BindView(R2.id.llCash)
    LinearLayout llCash;
    @BindView(R2.id.llTransfer)
    LinearLayout llTransfer;
    @BindView(R2.id.rlAward)
    RelativeLayout rlAward;
    @Autowired
    CoinAssetBean data;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_coin_info;
    }

    @Override
    public CoinInfoActivityPresenter getPresenter() {
        return new CoinInfoActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        setTitle(code + App.INSTANCE.getString(R.string.wallet_coinInfoActivity1_j));
        if (code.equals("CQTF") || code.equals("EXLC")) {
            rlAward.setVisibility(View.VISIBLE);
        }
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            if (userData != null && (code.equals("CQTF") || code.equals("EXLC"))) {
                mPresenter.checkAward(WalletUtil.walletBean.getAddr(), data.getCode());
//                mPresenter.checkAward(data.getAddress(),data.getCode());
            }
        });
        ArrayList<Fragment> fragments = new ArrayList<>();
        String[] titles = new String[]{App.INSTANCE.getString(R.string.wallet_coinInfoActivity2_j), App.INSTANCE.getString(R.string.wallet_coinInfoActivity3_j), App.INSTANCE.getString(R.string.wallet_coinInfoActivity4_j), App.INSTANCE.getString(R.string.wallet_coinInfoActivity5_j)};
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_COIN_RECORD)
                .withString(Constans.ADDRESS, address)
                .withString(Constans.CODE, code)
                .navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_COIN_RECORD)
                .withString(Constans.ADDRESS, address)
                .withString(Constans.CODE, code)

                .withString(Constans.TYPE, "1")

                .navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_COIN_RECORD)
                .withString(Constans.ADDRESS, address)
                .withString(Constans.CODE, code)

                .withString(Constans.TYPE, "2")
                .navigation());
        fragments.add((Fragment) ARouter.getInstance().build(RConfig.WALLET_FRAGMENT_COIN_RECORD)
                .withString(Constans.ADDRESS, address)
                .withString(Constans.CODE, code)
                .withString(Constans.STATE, "1")
                .navigation());

        slidingTabLayout.setViewPager(viewPager, titles, this, fragments);
        ivCoin.setImageResource(CoinEnum.getByCode(code).getRes());
//        ImageUtil.setImage(CoinEnum.getByCode(code).getRes(), ivCoin);
        tvAddress.setText(data.getAddress());
        tvCoin.setText(code);
        ClickUtil.click(ivCopy, () -> {
            CopyUtil.copy(data.getAddress());
        });
        tvUSDT.setText(data.getAvailable());
        tvRMB.setText(String.format("≈￥%s", data.getCnyAvailable()));
    }


    @Override
    protected void initEvent() {
        ClickUtil.click(llCash, () -> {
            ARouter.getInstance().build(RConfig.WALLET_CASH)
                    .withSerializable(Constans.DATA, data)
                    .navigation();
        });
        ClickUtil.click(llTransfer, () -> {
            ARouter.getInstance().build(RConfig.WALLET_TRANSFER)
                    .withString(Constans.ADDRESS, address)
                    .withString(Constans.CODE, code)
                    .withSerializable(Constans.DATA, data)
                    .navigation();
        });
        ClickUtil.click(ivClose, () -> {
            rlAward.setVisibility(View.GONE);
        });
    }

    public void updateAward(AwardBean data) {
        tvDesc.setText(String.format(App.INSTANCE.getString(R.string.wallet_coinInfoActivity6_j), code, data.getTotal_num(), data.getReward_num()));

        if (data.getIsreward().equals("1") && Double.valueOf(this.data.getAvailable()) >= Double.valueOf(data.getTotal_num())) {
            TipUtil.showAward(this, String.format(App.INSTANCE.getString(R.string.wallet_coinInfoActivity7_j) +
                    App.INSTANCE.getString(R.string.wallet_coinInfoActivity8_j), code, data.getReward_num()), () -> {

            });

        }
    }
}
